﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace tamlytre.Admin
{
    public partial class EditNews : System.Web.UI.Page
    {
        DBTuVanDataContext db = new DBTuVanDataContext();
        public static int idnews = 0;
        public static TB_User taiKhoan = null;
        public static string filename;
        public string CollectData()
        {
            taiKhoan = (TB_User)Session["TaiKhoan"];

            return taiKhoan.UserName.Trim();

        }
        
        public void loadnews()
        {
            bool bSuc1 = Int32.TryParse(Request.QueryString["idedit"], out idnews);
            if (bSuc1 == false)
            {
                Response.Redirect("../Admin/PostNews.aspx");
                return;
            }
            try
            {
                using (DBTuVanDataContext db = new DBTuVanDataContext())
                {
                    List<TB_Catagory> cate = db.USP_LoadAllCategory().ToList();
                    DropDownListChude.DataSource = cate;
                    DropDownListChude.DataValueField = "CateID";
                    DropDownListChude.DataTextField = "CateName";
                    DropDownListChude.DataBind();
                    TB_New news = db.TB_News.Single(n => n.idNews == idnews);
                    txtTieude.Text = news.Title;
                    DropDownListChude.SelectedValue = news.Cate_ID.ToString();
                    FCKTitle.Value = news.MainContent;
                    FCKContent.Value = news.Content;
                    txtTuKhoa.Text = news.TitleNosign;
                    txtkeyword.Text = news.Keyword;
                    txtGhichuhinh.Text = news.PictureNote;
                    txtLink.Text = news.Link;
                    txtTacGia.Text = news.Author;
                }
            }
            catch
            {
 
            }

        }

        public void uppicture()
        {
            HttpPostedFile myFile = FileUploadPicture.PostedFile;
            if (myFile.ContentLength / (1024 * 1024) > 2)
            {
                LabelMessage.Text = "Kích thước file lớn hơn 2Mb. Chọn một file khác !";
            }
            else
            {
                filename = myFile.FileName;
                bool checkfilename = db.TB_News.ToList().Exists(pt => pt.Picture == filename);
                if (checkfilename == true)
                {
                    filename = "1_" + filename;
                }
                myFile.SaveAs(Server.MapPath("../images/" + filename));
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                loadnews();
        }

        protected void ibtDangbai_Click(object sender, ImageClickEventArgs e)
        {
            bool bSuc1 = Int32.TryParse(Request.QueryString["idedit"], out idnews);
            if (bSuc1 == false)
            {
                Response.Redirect("../Admin/Default.aspx");
                return;
            }
            try
            {
                using (DBTuVanDataContext db = new DBTuVanDataContext())
                {
                    TB_New news = db.TB_News.Single(id => id.idNews == idnews);
                    if (DropDownListChude.SelectedItem != null)
                    {
                        news.Cate_ID = Convert.ToInt32(DropDownListChude.SelectedValue.Trim());
                    }
                    news.Title = txtTieude.Text;
                    news.MainContent = FCKTitle.Value;
                    news.Content = FCKContent.Value;
                    news.CreateDate = DateTime.Now;
                    news.PublishDate = DateTime.Now;
                    news.Author = txtTacGia.Text;
                    news.Keyword = txtkeyword.Text;
                    news.PictureNote = txtGhichuhinh.Text;
                    news.Link = txtLink.Text;
                    if (cbChonTieuDiem.Checked)
                        news.NewsFocus = 1;
                    else
                        news.NewsFocus = 0;
                    news.Status = 1;
                    if (cbBaiHay.Checked)
                        news.BaiHay = true;
                    else
                        news.BaiHay = false;
                    if (cbChangePicture.Checked)
                    {
                        uppicture();
                        news.Picture = filename;
                    }
                    news.TitleNosign = txtTuKhoa.Text;
                    news.User_Name = CollectData().Trim();
                    news.UserPublish = CollectData().Trim();
                    news.DaXoa = false;
                    db.SubmitChanges();
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script language='javascript'>alert('Sửa bài đăng thành công !');</script>");
                }
            }
            catch
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script language='javascript'>alert('Sửa bài đăng thất bại !');</script>");
                return;
            }
        }

        protected void ibtLuuKhongDang_Click(object sender, ImageClickEventArgs e)
        {
            bool bSuc1 = Int32.TryParse(Request.QueryString["idedit"], out idnews);
            if (bSuc1 == false)
            {
                Response.Redirect("../Admin/Default.aspx");
            }
            try
            {
                using (DBTuVanDataContext db = new DBTuVanDataContext())
                {
                    
                    TB_New news = db.TB_News.Single(id => id.idNews == idnews);
                    if (DropDownListChude.SelectedItem != null)
                    {
                        news.Cate_ID = Convert.ToInt32(DropDownListChude.SelectedValue.Trim());
                    } 
                    news.Title = txtTieude.Text;
                    news.MainContent = FCKTitle.Value;
                    news.Content = FCKContent.Value;
                    news.CreateDate = DateTime.Now;
                    news.Author = txtTacGia.Text;
                    news.Keyword = txtkeyword.Text;
                    news.PictureNote = txtGhichuhinh.Text;
                    news.Link = txtLink.Text;
                    if (cbChonTieuDiem.Checked)
                        news.NewsFocus = 1;
                    else
                        news.NewsFocus = 0;
                    if (cbBaiHay.Checked)
                        news.BaiHay = true;
                    else
                        news.BaiHay = false;
                    news.Status = 0;
                    if (cbChangePicture.Checked)
                    {
                        uppicture();
                        news.Picture = filename;
                    }
                    news.TitleNosign = txtTuKhoa.Text;
                    news.User_Name = CollectData().Trim();
                    news.DaXoa = false;
                    db.SubmitChanges();
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script language='javascript'>alert('Sửa và lưu thành công !');</script>");
                }
            }
            catch
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script language='javascript'>alert('Sửa và lưu thất bại !');</script>");
                return;
            }
        }
    }
}